//
//  HMWelcomeViewController.swift
//  Weibo
//
//  Created by xiongminghua on 16/7/24.
//  Copyright © 2016年 熊明华. All rights reserved.
//

import UIKit
import SnapKit

// 欢迎界面
class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        
    }
    
    override func viewDidAppear(animated: Bool) {
        
        startAnimation()
    }
    
    /// 头像动画
    private func startAnimation() {
        
        let offset = -(UIScreen.mainScreen().bounds.height - 160)
        // 通过改变约束来实现动画效果
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view).offset(offset)
        }
        
        // 使用UIView的弹簧动画效果
        UIView.animateWithDuration(1, delay: 0.2, usingSpringWithDamping: 0.75, initialSpringVelocity: 7, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 重写布局子控件
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                // 动画完成, 显示label
                UIView.animateWithDuration(DefaultAnimtionDuration, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        let controller = HMTabBarController()
                        // 切换界面
                        AppDelegate.swithRootViewController(controller)
                })
        }
    }
    

    /// 设置UI界面
    private func setupUI() {
        
        // 添加子控件
        self.view.addSubview(bkgImageView)
        self.view.addSubview(iconView)
        self.view.addSubview(welcomeLabel)
        
        // 添加约束
        
        // 背景
        bkgImageView.snp_makeConstraints { (make) -> Void in
            // 填充父控件
           // make.leading.trailing.top.bottom.equalTo(self.view)
            
            // 当四边都一样时
            make.edges.equalTo(self.view)
        }
        
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
        }
        
        // label
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    
    // MARK: - 懒加载
    
    /// 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像
    private lazy var iconView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 设置,切圆
        imageView.layer.cornerRadius = 42.5
        
        return imageView
    }()
    
    /// 欢迎label
    private lazy var welcomeLabel: UILabel = {
       
        let label = UILabel()
        
        // 设置
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(18)
        
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
    }()
    
}
